package com.example.dzcfinal;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.dzcfinal.SQLite.MySQLite;

public class RegisterActivity extends Activity implements TextWatcher{
    private Button jumpLogin;
    private Button RegisterBtn;
    private EditText username_register;
    private EditText password_register;
    private EditText checkPassword;
    private String usernameText;
    private String passwordText;
    private String checkText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initHandle();
        setJumpLogin();
        clickRegister();
    }

    private void clickRegister(){
        RegisterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                usernameText = username_register.getText().toString().trim();
                passwordText = password_register.getText().toString().trim();
                checkText = checkPassword.getText().toString().trim();
                if (usernameText.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                }else if (passwordText.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else if(!(passwordText.equals(checkText))){
                    Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                }else {
                    MySQLite sql = new MySQLite(RegisterActivity.this);
                    if (sql.findUser(usernameText)!=-1){
                        Toast.makeText(RegisterActivity.this, "用户已存在", Toast.LENGTH_SHORT).show();
                    }else {
                        sql.insertUser(usernameText,passwordText);
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });
    }

    protected void initHandle(){
        jumpLogin = findViewById(R.id.jumpLogin);
        username_register = findViewById(R.id.username_register);
        password_register = findViewById(R.id.password_register);
        checkPassword = findViewById(R.id.checkPassword);
        RegisterBtn = findViewById(R.id.RegisterBtn);

        username_register.addTextChangedListener(this);
        password_register.addTextChangedListener(this);
        checkPassword.addTextChangedListener(this);
    }


    protected void setJumpLogin(){
        jumpLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        usernameText = username_register.getText().toString().trim();
        passwordText = password_register.getText().toString().trim();
        checkText = checkPassword.getText().toString().trim();
        Log.e("TAG", "afterTextChanged: " +usernameText);
        Log.e("TAG", "afterTextChanged: " +passwordText);
        Log.e("TAG", "afterTextChanged: " +checkText);
        if (!(usernameText.equals(""))&&!(passwordText.equals(""))&&!(checkText.equals(""))){
            RegisterBtn.setBackgroundColor(Color.parseColor("#FF58B3DB"));
            RegisterBtn.setTextColor(Color.parseColor("#ffffff"));
        }else {
            RegisterBtn.setBackgroundColor(Color.parseColor("#d6d7d7"));
            RegisterBtn.setTextColor(Color.parseColor("#000000"));
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}